//
//  radioDetailViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-4.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "radioDetailViewController.h"
#import "radioDetailView.h"
#import <AVFoundation/AVFoundation.h>
#import "RadioHandel.h"
#import "radioDetailDataModel.h"
#import "RadioDataModel.h"
#import "NetRequest.h"
#import "UIImage+ImageDown.h"


#import "DownRadio.h"

#import "UMSocial.h"

@interface radioDetailViewController ()<AVAudioPlayerDelegate,UIAlertViewDelegate>
@property(nonatomic,retain)radioDetailView *rv;

//@property(nonatomic,retain)AVAudioPlayer *player;
//@property(nonatomic,)
@property(nonatomic,retain)NSArray *array;

@property(nonatomic,assign)BOOL t;

@property(nonatomic,retain)CABasicAnimation *rotationAnimation;


//@property(nonatomic,retain) AVPlayer *newPlayer;

@end

@implementation radioDetailViewController


-(void)dealloc
{
    [_rv release];
   
    [super dealloc];
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.rv =[[[radioDetailView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
        
        self.array = [[[NSArray alloc] init] autorelease];
        
        
    }
    return self;
}

-(void)loadView
{
    self.view = self.rv;
}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
  
    
    
    //音量进度条音量的 通知中心 接收当前时间的接受
      //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setProgress) name:@"progress" object:nil];
       
    
    self.t=YES;
    
   //系统自带的播放器AvAudioPlayer 不能播放网络请求下来的数据,只能播放本地的音乐;
    // AVPlayer *newPlayer = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:];
    //跳出模态视图
    [self.rv.leftButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    //开始
    [self.rv.starButton addTarget:self action:@selector(starButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //分享
    [self.rv.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //下载
    [self.rv.getButton addTarget:self action:@selector(getButtonAcction:) forControlEvents:UIControlEventTouchUpInside];
    //进度||声音
    [self.rv.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //请求详细页面的数据;不能在此调用解析数据;
    [[RadioHandel shareRadioData] requestDetailRadioData];
    //KVO检测详细页面的数据
    [[RadioHandel shareRadioData ] addObserver:self forKeyPath:@"RadioDetailDataArray" options:NSKeyValueObservingOptionNew context:nil];
    
}

#pragma mark 通知中心接受者调用的赋值方法
//-(void)setProgress  //progress 来自单利中定义的存
//{
//
//    self.rv.slider.maximumValue = [[RadioHandel shareRadioData] totalTimeValue];
//    self.rv.slider.value = [RadioHandel shareRadioData].progress;
//
//    
//}

//KVO方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{    if ([keyPath isEqualToString:@"RadioDetailDataArray"]) {
    
         [self getDetailRadioData];          
         [[RadioHandel shareRadioData] removeObserver:self forKeyPath:@"RadioDetailDataArray" context:nil];
    }
    
}


//赋值方法
-(void)getDetailRadioData
{
    radioDetailDataModel *rddm = [RadioHandel shareRadioData].RadioDetailDataArray[0];
    
    self.rv.titleLab.text = rddm.title;
    self.rv.rightLabel.text =[[NSString stringWithFormat:@"%ld",(long)rddm.replyCount] stringByAppendingString:@"💗"];
   
   //self.rv.imv.image = [UIImage imageNamed:@"radio.png"];
   
    self.rv.imv.image = [UIImage imageWithUrlString:rddm.cover myBlock:^(UIImage *image) {
        self.rv.imv.image = image;
       }];
   //(NSString *) self.rv.slider.minimumValue = [[RadioHandel shareRadioData].streamer currentTime];
    
}
//下载事件
-(void)getButtonAcction:(UIButton *)sender
{
   
       UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"亲爱的你想:" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"下载", nil];
    
    
    [Alert show];
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    radioDetailDataModel *rddm = [RadioHandel shareRadioData].RadioDetailDataArray[0];
    
    //判断点击的button的下标来实现响应的时间;分别写方法实现响应的功能
    if (buttonIndex == 0) {
        NSLog(@"我要取消 %d",buttonIndex);
        
        
    }else if(buttonIndex == 1){
        NSLog(@"我要下载 %d",buttonIndex);
        
        [[DownRadio downLoad] loadDownWithUrl:rddm.url_mp4 appendPath:rddm.title];
        
      }
}


//分享事件
-(void)shareButtonAction:(UIButton *)sender
{
    
    NSLog(@"分享给");
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"537bfc9456240b066602f82b" shareText:[NSString stringWithFormat:@"%@:%@",nil,nil] shareImage:nil  shareToSnsNames:@[UMShareToSina,UMShareToRenren] delegate:self];
    
}
//跳出模态停止播放
- (void)buttonAction:(UIButton *)sender
{
    //NSLog(@"haha");
    //播放暂停;
    [[RadioHandel shareRadioData].player pause];
    
    [RadioHandel shareRadioData].player = nil;
    
    
#pragma mark 在这里将所有的播放内容置空.    于此同时如果后面的下载界面的 cell 点击时 还得将下载的内容赋值     到页面中.viewWillAppear 
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
//控制滑块
-(void)sliderAction:(UISlider *)sender
{
      NSLog(@"%f",self.rv.slider.value);
    
    self.rv.slider.minimumValue = 1;
    self.rv.slider.maximumValue = 20;
    [RadioHandel shareRadioData].player.volume = self.rv.slider.value;
    
    
//    CMTime newTime = CMTimeMake(self.rv.slider.value, 1.0);
//    [[RadioHandel shareRadioData].player  seekToTime:newTime];
//    [[RadioHandel shareRadioData].player  play];
}

-(void)dongHua{
    
    //动画效果 .
    self.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    self.rotationAnimation.delegate = self;
    self.rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2];
    self.rotationAnimation.duration = 8;
    self.rotationAnimation.repeatCount = 1000;
    self.rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.rotationAnimation setValue:@"rotationAnimation" forKey:@"MyAnimationType"];
  //  [self.rv.imv.layer addAnimation:self.rotationAnimation forKey:@"rotationAnimation"];

}

//播放事件
-(void)starButtonAction:(UIButton *)sender
{
    //开始播放:需要用到url_mp4 的电台接口
    
    radioDetailDataModel *rddm = [RadioHandel shareRadioData].RadioDetailDataArray.lastObject;
    
    if ([sender.currentBackgroundImage  isEqual:[UIImage imageNamed:@"pause.png"]] ) {
        
        //这里的rate 可以监测出该音频的播放状态是否空闲.空闲就重放.
        if (self.t || [RadioHandel shareRadioData].player.rate == 1.0) {//缺少一个判断播放完成之后的空闲状态.
            
            [[RadioHandel shareRadioData] StarPlayerWithString:rddm.url_mp4];

            self.t = NO;
            
            [self dongHua];
            //开始动画
            [self.rv.imv.layer addAnimation:self.rotationAnimation forKey:@"rotationAnimation"];
            
        }else{
            
            [[RadioHandel shareRadioData].player play];
            //开始动画
            [self.rv.imv.layer addAnimation:self.rotationAnimation forKey:@"rotationAnimation"];
        }
        
       
         [sender setBackgroundImage:[UIImage imageNamed:@"start.png"] forState:UIControlStateNormal];
       
    }else{
        
        [[RadioHandel shareRadioData].player pause];
        
        [sender setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
        
        //取消动画
        [self.rv.imv.layer removeAllAnimations];
    }
    //有一个问题,当MP3播放完了之后....
}





#pragma mark 三个方法控制旋转只为竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
